package com.gitee.l0km.beanfilter.core.json;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerBuilder;

/**
 * @author guyadong
 */
class JacksonBeanSerializerBuilder extends BeanSerializerBuilder {
	private final static BeanPropertyWriter[] NO_PROPERTIES = new BeanPropertyWriter[0];
	JacksonBeanSerializerBuilder(BeanDescription beanDesc) {
		super(beanDesc);
	}

	JacksonBeanSerializerBuilder(BeanSerializerBuilder src) {
		super(src);
	}
    public void setConfig(SerializationConfig config) {
        _config = config;
    }
    JacksonBeanSerializerBuilder builder()
    {
        if (_anyGetter != null) {
            _anyGetter.fixAccess(_config);
        }
        if (_typeId != null) {
            if (_config.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)) {
                _typeId.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
            }
        }
        return this;
    }
    /**
     * @see #build()
     */
    BeanPropertyWriter[] buildProperties() {
        BeanPropertyWriter[] properties;
        // No properties, any getter or object id writer?
        // No real serializer; caller gets to handle
        if (_properties == null || _properties.isEmpty()) {
            if (_anyGetter == null && _objectIdWriter == null) {
                return null;
            }
            properties = NO_PROPERTIES;
        } else {
            properties = _properties.toArray(new BeanPropertyWriter[_properties.size()]);
            if (_config.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)) {
                for (int i = 0, end = properties.length; i < end; ++i) {
                    properties[i].fixAccess(_config);
                }
            }
        }
        return properties;
    }
}
